package WorldBorder;

import java.util.ArrayList;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import com.SelfHome.Main;
import com.SelfHome.Variable;
import com.Util.Home;
import com.Util.HomeAPI;
import com.Util.Util;

public class WBControl {
	

	public static HashMap<String,Integer> border_redis = new HashMap<String,Integer>();
	
		public static void setEnable(Player p) {
			
			if(Variable.not_adopt_nms == true) {
				return;
			}
			
			if(!Util.CheckIsHome(p.getWorld().getName())) {
				return;
			}
			String v = Bukkit.getServer().getClass().getPackage().getName();
			v = v.substring(v.lastIndexOf('.') + 1);
			
			
			try {
				if(v.contains("v1_12_R1")) {
					WorldBorder.R_12_1.hide(p, p.getWorld());
				}else if(v.contains("v1_16_R1")) {
					WorldBorder.R_16_1.hide(p, p.getWorld());
				}else if(v.contains("v1_16_R2")) {
					WorldBorder.R_16_2.hide(p, p.getWorld());
				}else if(v.contains("v1_16_R3")) {
					WorldBorder.R_16_3.hide(p, p.getWorld());
				}
			}catch(NoSuchFieldError e) {
				Variable.not_adopt_nms = true;
			}


		}
		public static void setDisable(Player p) {
	
			if(Variable.not_adopt_nms == true) {
				return;
			}
			
			if(!Util.CheckIsHome(p.getWorld().getName())) {
				return;
			}
			String v = Bukkit.getServer().getClass().getPackage().getName();
			v = v.substring(v.lastIndexOf('.') + 1);
			Home home = HomeAPI.getHome(p.getWorld().getName());
			
			
			   int vip_add = 0;
		    	  ArrayList<String> players = new ArrayList<String>();
		    	  players.add(home.getName());
		    	  for(String op:home.getOPs()) {
		    		  if(Bukkit.getPlayer(op) != null) {
		    			  players.add(op);
		    		  }
		    	  }
		          for(String s:Main.JavaPlugin.getConfig().getStringList("VIPAdd")) {
		        	  String[] ss = s.split(",");
		        	  for(String p_name : players) {
		        		  Player pe = Bukkit.getPlayer(p_name);
		        		  if(pe==null) {
		        			  continue;
		        		  }
		        		  if(pe.hasPermission(ss[0])) {
		        			  int add = Integer.valueOf(ss[1]);
		        			  if(add > vip_add) {
		        				  vip_add = add;
		        			  }
		        		  }
		        		  
		        	  }
		          }
			
		          if(border_redis.containsKey(home.getName())) {
		        	  if(vip_add < border_redis.get(home.getName())) {
		        		  vip_add = border_redis.get(home.getName());
		        	  }else {
		        		  border_redis.put(home.getName(), vip_add);
		        	  }
		          }else {
		        	  border_redis.put(home.getName(), vip_add);
		          }
		          
			
			try {
				if(v.contains("v1_12_R1")) {
					WorldBorder.R_12_1.show(p, p.getWorld(),p.getWorld().getSpawnLocation().getX(),p.getWorld().getSpawnLocation().getZ(),(Main.JavaPlugin.getConfig().getInt("WorldBoard") + vip_add + (home.getLevel() - 1) * Main.JavaPlugin.getConfig().getInt("UpdateRadius")));
				}else if(v.contains("v1_16_R1")) {
					WorldBorder.R_16_1.show(p, p.getWorld(),p.getWorld().getSpawnLocation().getX(),p.getWorld().getSpawnLocation().getZ(),(Main.JavaPlugin.getConfig().getInt("WorldBoard") + vip_add + (home.getLevel() - 1) * Main.JavaPlugin.getConfig().getInt("UpdateRadius")));
				}else if(v.contains("v1_16_R2")) {
					WorldBorder.R_16_2.show(p, p.getWorld(),p.getWorld().getSpawnLocation().getX(),p.getWorld().getSpawnLocation().getZ(),(Main.JavaPlugin.getConfig().getInt("WorldBoard") + vip_add + (home.getLevel() - 1) * Main.JavaPlugin.getConfig().getInt("UpdateRadius")));
				}else if(v.contains("v1_16_R3")) {
					WorldBorder.R_16_3.show(p, p.getWorld(),p.getWorld().getSpawnLocation().getX(),p.getWorld().getSpawnLocation().getZ(),(Main.JavaPlugin.getConfig().getInt("WorldBoard") + vip_add + (home.getLevel() - 1) * Main.JavaPlugin.getConfig().getInt("UpdateRadius")));
				}	
			}catch(NoSuchFieldError e) {
				Variable.not_adopt_nms = true;
			}


		}
		
		public static void togglecc(Player p) {
			
			if(Variable.not_adopt_nms == true) {
				return;
			}
			
			if(!(Util.CheckIsHome(p.getWorld().getName()))) {
				return;
			}
			
		  	if(!Variable.has_already_hide_border.contains(p.getName())) {
		  		setEnable(p);
		  		p.sendMessage(Variable.Lang_YML.getString("ToggleccWorldDisable"));
		  		Variable.has_already_hide_border.add(p.getName());
        	}else {
        		setDisable(p);
        		p.sendMessage(Variable.Lang_YML.getString("ToggleccWorldEnable"));
        		Variable.has_already_hide_border.remove(p.getName());
        	}	
   
		}
		
}
